package com.zhangtai.modules.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dto.SalaryChangeDto;
import com.zhangtai.modules.entity.salary.SalaryChangeEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 薪资变动
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-09-17 20:48:44
 */
@Mapper
public interface SalaryChangeDao extends BaseMapper<SalaryChangeEntity> {

    @Select(
            "<script>SELECT\n" +
                    "\ttsc.*,\n" +
                    "\ttei.NAME,\n" +
                    "\ttei.CODE,\n" +
                    "\ttei.unit_id,\n" +
                    "\ttei.unitname,\n" +
                    "\ttei.org_id,\n" +
                    "\ttei.orgname,\n" +
                    "\ttei.job_id,\n" +
                    "\ttei.jobname,\n" +
                    "\ttei.job_level,\n" +
                    "\ttei.job_level_type,\n" +
                    "\ttei.job_Level_id \n" +
                    "FROM\n" +
                    "\ttb_salary_change tsc\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tsc.oid = tei.oid " +
//                    "\tLEFT JOIN tb_salary_group_employee tsge ON tsge.emp_code = tei.code " +
                    "<where> tsc.is_delete = 0 " +
                    "<if test='orgIds!=null and orgIds.size>0'> and ( tei.unit_id in " +
                    " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
                    " or tsc.apply_code = '${applyCode}' ) " +
                    "</if>"+
                    "<if test='applyCode==null or orgIds.size==0'> and (tei.org_id = null or tsc.apply_code = '${applyCode}')</if>" +
                    "<if test='orgName!=null'> and tei.orgname like '%${orgName}%' </if>" +
                    "<if test='unitName!=null'> and tei.unitname like '%${unitName}%' </if>" +
                    "<if test='name!=null'> and tei.name like '%${name}%' </if>" +
                    "<if test='code!=null'> and tei.code like '%${code}%' </if>" +
                    "<if test='type!=null'> and tsc.type = #{type} </if>" +
                    "<if test='approveStatus!=null'> and tsc.approve_status = #{approveStatus} </if>" +
                    "<if test='approveStatus==null'> and tsc.approve_status != '2'  and tsc.approve_status != '4'</if>" +
                    "<if test='status!=null'> and tsc.status = #{status} </if>" +
                    "</where>  ORDER BY tsc.create_time DESC " +
                    "</script>"
    )
    List<SalaryChangeDto> getSalaryChangeList(
            @Param("orgIds") List<Long> orgIds,
            @Param(value = "name") String name,
            @Param(value = "orgName") String orgName,
            @Param(value = "unitName") String unitName,
            @Param(value = "code") String code,
            @Param(value = "applyCode") String applyCode,
            @Param(value = "type") Integer type,
            @Param(value = "approveStatus") Integer approveStatus,
            @Param(value = "status") Integer status,
            Page<SalaryChangeDto> page
    );

    @Select(
            "<script>SELECT\n" +
                    "\ttsc.*,\n" +
                    "\ttei.NAME,\n" +
                    "\ttei.CODE,\n" +
                    "\ttei.unit_id,\n" +
                    "\ttei.unitname,\n" +
                    "\ttei.org_id,\n" +
                    "\ttei.orgname,\n" +
                    "\ttei.job_id,\n" +
                    "\ttei.jobname,\n" +
                    "\ttei.job_level,\n" +
                    "\ttei.job_level_type,\n" +
                    "\ttei.job_Level_id \n" +
                    "FROM\n" +
                    "\ttb_salary_change tsc\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tsc.oid = tei.oid " +
                   "\tLEFT JOIN tb_salary_group_employee tsge ON tsge.emp_code = tei.code " +
                    "<where> tsc.is_delete = 0 " +
                    "<if test='orgIds!=null and orgIds.size>0'> and ( tsge.group_id in " +
                    " <foreach item=\"orgId\" index=\"index\" collection=\"orgIds\" open=\"(\" separator=\",\" close=\")\">#{orgId}</foreach>" +
                    " or tsc.apply_code = '${applyCode}' ) " +
                    "</if>"+
                    "<if test='applyCode==null or orgIds.size==0'> and (tei.org_id = null or tsc.apply_code = '${applyCode}')</if>" +
                    "<if test='orgName!=null'> and tei.orgname like '%${orgName}%' </if>" +
                    "<if test='unitName!=null'> and tei.unitname like '%${unitName}%' </if>" +
                    "<if test='name!=null'> and tei.name like '%${name}%' </if>" +
                    "<if test='code!=null'> and tei.code like '%${code}%' </if>" +
                    "<if test='type!=null'> and tsc.type = #{type} </if>" +
                    "<if test='approveStatus!=null'> and tsc.approve_status = #{approveStatus} </if>" +
                    "<if test='approveStatus==null'> and tsc.approve_status != '2'  and tsc.approve_status != '4'</if>" +
                    "<if test='status!=null'> and tsc.status = #{status} </if>" +
                    "</where>  ORDER BY tsc.create_time DESC " +
                    "</script>"
    )
    List<SalaryChangeDto> getSalaryChangeConfirmList(
            @Param("orgIds") List<Long> orgIds,
            @Param(value = "name") String name,
            @Param(value = "orgName") String orgName,
            @Param(value = "unitName") String unitName,
            @Param(value = "code") String code,
            @Param(value = "applyCode") String applyCode,
            @Param(value = "type") Integer type,
            @Param(value = "approveStatus") Integer approveStatus,
            @Param(value = "status") Integer status,
            Page<SalaryChangeDto> page
    );


    @Select(
            "<script>SELECT\n" +
                    "\ttsc.*,\n" +
                    "\ttei.NAME,\n" +
                    "\ttei.CODE,\n" +
                    "\ttei.unit_id,\n" +
                    "\ttei.unitname,\n" +
                    "\ttei.org_id,\n" +
                    "\ttei.orgname,\n" +
                    "\ttei.job_id,\n" +
                    "\ttei.jobname,\n" +
                    "\ttei.job_level,\n" +
                    "\ttei.job_level_type,\n" +
                    "\ttei.job_Level_id \n" +
                    "FROM\n" +
                    "\ttb_salary_change tsc\n" +
                    "\tLEFT JOIN tb_employee_info tei ON tsc.oid = tei.oid " +
                    "<where> tsc.is_delete = 0 " +
                    "<if test='oid!=null'> and tsc.oid = #{oid} </if>" +
                    "<if test='dateStart!=null and dateStart!=&apos;&apos;'>and tsc.create_time &gt;= #{dateStart} and tsc.create_time &lt;=#{dateEnd} </if>" +
                    "</where>  ORDER BY tsc.create_time DESC " +
                    "</script>"
    )
    List<SalaryChangeDto> getSalaryChangePage(
            @Param(value = "oid") String oid,
            @Param(value = "dateStart") String dateStart,
            @Param(value = "dateEnd") String dateEnd,
            Page<SalaryChangeDto> page
    );

}
